// Flamer v1.0.1
// flamer.txt (modification on t13kimzahn.txt from the Za-Khazi Run)
// by Kelandon (tomwatts@berkeley.edu)
//
// This is a generic version of Kim Zahn's script from ZKR. The monster blasts
// fire periodically while in combat, hurting anyone nearby.
//
// The script can call a town state when the creature dies (much like my Special
// Death script).
//
// NOTE: The message_dialog right now is set for a masculine monster. Edit as
// necessary.
//
// Memory Cells:
//   Cell 0 - How creature moves.
//     0 - If 0, wander randomly. 
//     1 - Stands still until a target appears.
//     2 - Completely immobile, even if target appears.
//   Cell 1 - Which town state to call when the creature dies. If left at 0,
//		none.
//   Cell 2 - What town state to call when first exploding. If left at 0, none.
//   Cell 3 - Dialogue node to start with if talked to. if left at 0, this
//     character doesn't talk.
//	 Cell 4,5,6 - Amount of damage to do when using the ability. The ability
//		does the sum of (cell 4) number of random numbers in the range between
//		(cell 5) and (cell 6). So if cells 4, 5, and 6 are 25, 1, and 10,
//		respectively, this creature will do damage equal to the sum of 
//		twenty-five random numbers between 1 and 10 (which is what Kim Zahn
//		did). If left at 0, cell 4 defaults to 1 and cell 6 defaults to
//		10.
//	 Cell 7 - Radius of damage. Anything within this number of spaces of the
//		character takes the damage specified above when the ability is used. If
//		left at 0, defaults to 4.
//	 Cell 8 - How many ticks elapse in between using the flame ability. (So if
//		this is 0, the character will use the ability once every turn.)
//		NOTE: Generally this character will use the ability when it first sees
//		the party, unless the party finds the character almost immediately after
//		entering the town. The delay in cell 8 is only AFTER the first use.

begincreaturescript;

variables;

short i,target;
short last_abil_time;
short pc_targ;
short dialog = 1;

body;

beginstate INIT_STATE;
	// If any of the critical memory cells are 0, set them to real values.
	if (get_memory_cell(4) == 0)
		set_memory_cell(4,1);
	if (get_memory_cell(6) == 0)
		set_memory_cell(6,10);
	if (get_memory_cell(7) == 0)
		set_memory_cell(7,4);
		
	if (get_memory_cell(0) == 2)
		set_mobility(ME,0);
		
	last_abil_time = get_current_tick();
break;

beginstate DEAD_STATE;
	// Call a town state.
	if (get_memory_cell(1) > 0)
		run_town_script(get_memory_cell(1));
break;

beginstate START_STATE; 
	// if I have a target for some reason, go attack it
	if (target_ok()) {
		if (dist_to_char(get_target()) <= 16)
			set_state(3);
			else set_target(ME,-1);
		}
	
	// Look for a target, attack it if visible
	if (select_target(ME,8,0)) {
		do_attack();
		set_state(3);
		}
		
	// Have I been hit? Strike back!
	if (who_hit_me() >= 0) {
		set_target(ME,who_hit_me());
		do_attack();
		set_state(3);
		}
		else if (get_memory_cell(0) == 0) {
			fidget(ME,25);
			}

	// if we're in combat and the above didn't give me anything to do, just
	// stop now. Otherwise, game will keep running script, and that eats up CPU time.
	if (am_i_doing_action() == FALSE)
		end_combat_turn();
break;

beginstate 3; // attacking
	// If the party is in sight and enough time has elapsed to use the ability,
	// use it.
	if ((can_see_char(1000)) && (tick_difference(last_abil_time,get_current_tick()) > get_memory_cell(8))) {
		// If the party hasn't been told what's going on, tell them.
		if (dialog) {
			dialog = 0;
			if (get_memory_cell(1) > 0)
				run_town_script(get_memory_cell(2));
			}
		
		// Explode. Do damage to characters nearby.
		print_named_str(ME,"explodes!");
		put_boom_on_char(ME,0,1);
		put_boom_on_char(ME,0,1);
		put_boom_on_char(ME,0,1);
		put_boom_on_char(ME,0,1);
		put_boom_on_char(ME,0,1);
		put_boom_on_char(ME,0,1);
		run_animation_sound(167);
		damage_near_loc(my_loc_x(),my_loc_y(),get_ran(get_memory_cell(4),get_memory_cell(5),get_memory_cell(6)),get_memory_cell(7),1); 
		
		last_abil_time = get_current_tick();
		}
	// Then proceed as normal. Attack, if there's anyone to attack.
	if (target_ok() == FALSE)
		set_state(START_STATE);
	do_attack();
break;

beginstate TALKING_STATE;
	if (get_memory_cell(3) == 0) {
		print_str("Talking: It doesn't respond.");
		end();
		}
	begin_talk_mode(get_memory_cell(3));
break;